package lincen.javame.game.handhelddevice;

import java.awt.Color;
import java.awt.Graphics;


public class Cells {

	/**
	 * 点宽
	 */
	public static final int CELLWIDTH = 10;

	static final Color CELL_COLOR = new Color(0x252525);
	
	private static final Color CELL_CLOSE_COLOR = new Color(0x888888);
	
	public static final Color BACK_COLOR = new Color(0x808080);
	
	/**
	 * 点阵集
	 */
	private boolean [] [] point;

	/**
	 * 点阵的横向数量
	 */
	private int nW;

	/**
	 * 点阵的纵向数量
	 */
	private int nH;
	
	private int x;
	
	private int y;
	
	Cells(int nW, int nH) {
		this.setNw(nW);
		this.setNh(nH);
		this.point = new boolean [nH] [nW];
	}
	/**
	 * 打开坐标为x, y的点
	 * @param x
	 * @param y
	 */
	void open(int x, int y) {
		set(x, y, true);
	}

	/**
	 * 关闭坐标为x, y的点
	 * @param x
	 * @param y
	 */
	void close(int x, int y) {
		set(x, y, false);
	}


	/**
	 * 清空上次所绘制的图形
	 */
	void clear() {
		if (this.point != null) {
			for (int i = 0; i < this.getNh(); i++) {
				for (int j = 0; j < this.getNw(); j++) {
					this.point[i][j] = false;
				}
			}
		}
	}

	/**
	 * 将缓冲信息绘制到被关联的画布中
	 */
	void paint(Graphics g) {
		if (g != null) {
			for (int y = 0; y < getNh(); y++) {
				for (int x = 0; x < getNw(); x++) {
					if (this.point[y][x]) {
						fillCell(x, y, g);
					} else {
						fillCellSpace(x, y, g);
					}
				}
			}
		}
	}
	
	/**
	 * 将点x, y的状态设置为openOrClose
	 * @param x
	 * @param y
	 * @param openOrClose
	 */
	private void set(int x, int y, boolean openOrClose) {
		if(this.point != null) {
			if(
					x >= 0 && x < this.getNw() && 
					y >= 0 && y < this.getNh()
			) {
				this.point [y] [x] = openOrClose;
			}
		}
	}

	/**
	 * 填充画布上横坐标为x, 纵坐标为y 的方块(打开状态)
	 * @param x
	 * @param y
	 * @param graphics
	 */
	private void fillCell(int x, int y, Graphics graphics) {
		Color colorBefor = graphics.getColor();
		graphics.setColor(Cells.CELL_COLOR);
		graphics.fillRect(
				this.getX() + x * CELLWIDTH ,
				this.getY() + y * CELLWIDTH,
				CELLWIDTH - 1, 
				CELLWIDTH - 1
		);
		graphics.setColor(colorBefor);
	}
	
	/**
	 * 填充画布上横坐标为x, 纵坐标为y 的方块(关闭状态)
	 * 关闭状态
	 * @param x
	 * @param y
	 * @param graphics
	 */
	private void fillCellSpace(int x, int y, Graphics graphics) {
		Color colorBefor = graphics.getColor();
		graphics.setColor(Cells.CELL_CLOSE_COLOR);
		graphics.drawRect(
				this.getX() + x * CELLWIDTH, 
				this.getY() + y * CELLWIDTH,
				CELLWIDTH - 2, 
				CELLWIDTH - 2
		);
		graphics.setColor(colorBefor);
	}

	public int getWidth() {
		return this.getNw() * CELLWIDTH;
	}
	
	public int getHeight() {
		return this.getNh() * CELLWIDTH;
	}
	
	public void setNw(int nW) {
		this.nW = nW;
	}
	
	public int getNw() {
		return nW;
	}
	
	public void setNh(int nH) {
		this.nH = nH;
	}
	
	public int getNh() {
		return nH;
	}
	
	public void setY(int y) {
		this.y = y;
	}
	
	public int getY() {
		return y;
	}
	
	public void setX(int x) {
		this.x = x;
	}
	
	public int getX() {
		return x;
	}

}
